package com.cyq.day23.date;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateSwing extends JFrame {
    private JButton jButton;
    private JLabel label1,label2;
    private JTextField jTextField1,jTextField2;
    private JPanel jpanel1,jpanel2;
    public DateSwing(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("时间计算");
        this.setLayout(new GridLayout(3,1,5,5));
        label1=new JLabel("请输入第一个时间");
        jTextField1=new JTextField(20);
        jpanel1=new JPanel();
        jpanel1.add(label1);
        jpanel1.add(jTextField1);
        label2=new JLabel("请输入第二个时间");
        jTextField2=new JTextField(20);
        jpanel2=new JPanel();
        jpanel2.add(label2);
        jpanel2.add(jTextField2);
        jButton=new JButton("确定");

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DateFormat format=new SimpleDateFormat("yyyyMMdd");
                try {
                    Date date1=format.parse(jTextField1.getText());
                    Date date2=format.parse(jTextField2.getText());
                    Calendar c1=Calendar.getInstance();
                    Calendar c2=Calendar.getInstance();
                    c1.setTime(date1);
                    c2.setTime(date2);
                    int dateResult=c2.get(Calendar.DATE)-c1.get(Calendar.DATE);
                    int monthResult=c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
                    int yearResult=c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
                    int result=365*yearResult+30*monthResult+dateResult;
                    JOptionPane.showMessageDialog(null,result);

                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
            }
        });
       this.add(jpanel1);
       this.add(jpanel2);
       this.add(jButton);

    }
}
